<?php
require('init.php');
$title = 'Next Contests Registation';
require(LIBWWWDIR . '/header.php');

?>
<!--
<script type="text/javascript" src="../jquery-validation-1.9.0/lib/jquery.js" charset="utf-8"></script>
<script type="text/javascript" src="../jquery-validation-1.9.0/jquery.validate.js" charset="utf-8"></script>
<script type="text/javascript" src="../jquery-validation-1.9.0/jquery.validate.min.js" charset="utf-8"></script>
<script type="text/javascript" src="../jquery-validation-1.9.0/additional-methods.js" charset="utf-8"></script> -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function(){

$("#put").click( function(event){ 
	var values = $('input:checkbox:checked.team').map(function (){return this.value;}).get();
	var team = $("#teams").val();
	if( ((values.length) != 0)  && ( team != 'select') ){
	//if( (values.length) != 0){
		//$("#team_select").val( team );
		$("#members").val( values );	
	}else{
		event.preventDefault();
	}
	});
});
</script>
<?php
//require(LIBWWWDIR . '/new_lib.php');
require(LIBWWWDIR . '/forms.php');

if( isset($_GET['c']) ){
	$cid = $_GET['c'];
	//$res = $DB->q('TUPLE SELECT categoryid, contestname FROM contest WHERE cid = %i',$cid);
	$res = $DB->q('TUPLE SELECT contestname FROM contest WHERE cid = %i',$cid);
	$cat = $res['categoryid'];
	$name = $res['contestname'];
	//$table = tab_ins_contest( $cat );
	//echo $table;
	$table = "ins_nat_training";
	//$conexion = connect();
	$query = "SELECT * FROM ".$table." WHERE answered=0 ";
	$ins = mysql_query($query, $conn_db1) or die(mysql_error());
	$tam_res = mysql_num_rows($ins);
	$resultados = array();
	for($i = 0; $i < $tam_res; $i++){
		$datos = mysql_fetch_array($ins, MYSQL_BOTH);
		$resultados[$i] = $datos;
	}
	echo $tam_res;
	print_r($resultados);
	echo "<h1>".$name."</h1>\n\n";
	if( $tam_res == '0' ){
		echo "<h2> No registered users yet</h2>\n\n";
	}else{
		echo "<h2> Registered users</h2>\n\n".
		"<table class=\"list sortable\">\n<thead>\n" .
		addform( 'enable_team_back.php', 'post','team' );
		//if( $cat == '1'){
			echo "<tr><th scope=\"col\">user</th><th scope=\"col\">cid</th>" .
			"<th scope=\"col\">registered time</th><th scope=\"col\">add to team</th>".
			"</tr>\n</tr>\n</thead>\n<tbody>\n";
			foreach($resultados as $row) {
				echo "<tr><td>".htmlspecialchars($row[0])."</td><td>".
				htmlspecialchars($row[1])."</td><td>".htmlspecialchars($row[2]).
				"</td><td><input type=\"checkbox\" id=\"put_team\" ".
				"name=\"put_team\" value=".$row[0]." class=\"team\">".$row[0]."</td>";
			}
			echo "</tbody>\n</table><br><br>\n\n";
			echo '<tr><td><input type="hidden" name="members" value="" id="members"></td><td>'.
			'<input type="hidden" name="team_select" value="" id="team_select"></td></tr>';
			echo addHidden("cid", $cid );
			$tmap = $DB->q("KEYVALUETABLE SELECT login,name FROM team");
			$tmap["select"] = "...";
			echo "<tr><td><label>Avaible teams: </label>".
			addSelect("teams", $tmap, "select", "true")."</td><td>".
			addSubmit('add users to team', 'put', 'team.put()')."</td></tr><br><br>";
echo "PUEDE HABILITAR TODOS LOS TEAMS, SÓLO CUANDO TODOS LOS INTEGRANTES DE LA LISTA DE INSCRITOS PERTENEZCA A UN TEAM";	
		/*}
		if( $cat == '2'  OR $cat == '4'){
			echo "<tr><th scope=\"col\">user</th><th scope=\"col\">cid</th>" .
			"<th scope=\"col\">registered time</th></tr>\n</tr>\n</thead>\n<tbody>\n";
			foreach($resultados as $row) {
				echo "<tr><td>".htmlspecialchars($row[0])."</td><td>".
				htmlspecialchars($row[1])."</td><td>".htmlspecialchars($row[2]).
				"</td><td>";
			}
			echo "</tbody>\n</table><br><br>\n\n";
		}
		if( $cat == '3' ){
		echo "<tr><th scope=\"col\">user</th><th scope=\"col\">cid</th>" .
		"<th scope=\"col\">teamname</th><th scope=\"col\">members</th>".
		"<th scope=\"col\">registered time</th></tr>\n".
		"</tr>\n</thead>\n<tbody>\n";
		foreach($resultados as $row) {
			echo "<tr><td>".htmlspecialchars($row[0])."</td><td>".htmlspecialchars($row[1]).
			"</td><td>".htmlspecialchars($row[2])."</td><td>".htmlspecialchars($row[3]).
			"</td><td>".$row[4]."</td><td>";
		}
		echo "</tbody>\n</table><br><br>\n\n";
		}	*/
		echo addHidden("tc", $cat ).
		"<tr><td>".addSubmit('Enable all teams', 'able', 'team.able()').
		"</td></tr>".
		addEndForm();	
	}

	require(LIBWWWDIR . '/footer.php');
	exit;	
}


echo "<h1> Next Contests Registation</h1>\n\n";

$res = $DB->q('TABLE SELECT * FROM contest WHERE post = 0 ORDER BY endtime DESC');

if( count($res) == 0 ) {
	echo "<p class=\"nodata\">No contests defined</p>\n\n";
} else {
	echo "<table class=\"list sortable\"><thead>\n" .
	     "<tr><th scope=\"col\">name</th><th scope=\"col\">active</th>".
	     "<th scope=\"col\">starts</th><th scope=\"col\">ends</th>" .
	     "</tr>\n</thead>\n<tbody>\n";

	$iseven = false;
	foreach($res as $row) {
		if( difftime( now(),$row['endtime']) < 0  AND difftime( now(),$row['activatetime']) < 0  ){
			
		//$link = '<a href="contest_h.php?id=' . urlencode($row['cid']) . '">';

		echo '<tr class="' .
			( $iseven ? 'roweven': 'rowodd' ) .
			(!$row['enabled']    ? ' disabled' :'') .
			($row['cid'] == $cid ? ' highlight':'') . '">' .
			"<td align=\"right\">".htmlspecialchars($row['contestname'])."</td>\n".
			"<td title=".htmlspecialchars(@$row['activatetime']) .">" .
			printtime($row['activatetime'])."X".
				$date = $row['activatetime'];
				$fecha = new DateTime($date);
				$fecha->sub(new DateInterval('PT00H30M00S'));
				echo $fecha->format('Y-m-d H:i:s') . "Y".			
			"</td>".
			"<td title=".htmlspecialchars(@$row['starttime']) .">" .
			printtime($row['starttime'])."</td>".
			"<td title=".htmlspecialchars(@$row['endtime']) .">" .
			printtime($row['endtime'])."</td>";
		$iseven = ! $iseven;
		$cid = $row['cid'];
		$tc = $row['categoryid'];
		echo '<td class="editdel">';
			echo '<a href="ins_next_contests.php?c='.$cid.'">'.
		'<img src="../images/view_list.png" alt="edit" title="Ver inscritos" class="picto"/></a>';
		echo '</td>\n';
	}
	}
	echo "</tbody>\n</table>\n\n";
}











require(LIBWWWDIR . '/footer.php');
?>
